<template>
  <div class="payment-flow-page">
    <div class="page-header">
      <div class="header-content">
        <h1>支付流水详情</h1>
        <button class="back-btn" @click="goBack">
          <i class="back-icon">←</i> 返回
        </button>
      </div>
    </div>

    <div v-if="loading" class="loading-state">
      <div class="spinner"></div>
      <p>正在加载流水信息...</p>
    </div>

    <div v-if="error" class="error-state">
      <p class="error-icon">⚠️</p>
      <p class="error-message">{{ error }}</p>
      <button @click="fetchFlow" class="retry-btn">重试</button>
    </div>

    <div v-if="!loading && !error && flowData" class="flow-container">
      <div class="status-card" :class="statusClass">
        <div class="status-icon">
          <span v-if="flowData.statues === 1">✅</span>
          <span v-else-if="flowData.statues === 2">⏳</span>
          <span v-else>❌</span>
        </div>
        <div class="status-info">
          <h2>{{ statusText }}</h2>
          <p>{{ statusDescription }}</p>
        </div>
      </div>

      <div class="flow-details">
        <h3>流水信息</h3>
        <ul class="detail-list">
          <!-- <li class="detail-item">
            <span class="label">流水ID</span>
            <span class="value">{{ flowData.transactionId }}</span>
          </li> -->
          <li class="detail-item">
            <span class="label">流水编号</span>
            <span class="value">{{ flowData.transactionNumber }}</span>
          </li>
          <!-- <li class="detail-item">
            <span class="label">关联订单ID</span>
            <span class="value">{{ flowData.orderId }}</span>
          </li> -->
          <li class="detail-item">
            <span class="label">支付金额</span>
            <span class="value">¥{{ flowData.paymentAmount.toFixed(2) }}</span>
          </li>
          <li class="detail-item">
            <span class="label">创建时间</span>
            <span class="value">{{ formatDate(flowData.createTime) }}</span>
          </li>
          <li class="detail-item" v-if="flowData.updateTime">
            <span class="label">更新时间</span>
            <span class="value">{{ formatDate(flowData.updateTime) }}</span>
          </li>
          <li class="detail-item" v-if="flowData.other1">
            <span class="label">支付凭证号</span>
            <span class="value">{{ flowData.other1 }}</span>
          </li>
        </ul>
      </div>

      <div class="action-buttons">
        <button @click="goToHome" class="home-btn">返回首页</button>
      </div>
    </div>






<!-- 猜你想看 -->
<section class="guess-section">
  <div class="section-header">
    <h2>猜你想看</h2>
  </div>

  <div class="movies-list">
    <div
      v-for="movie in recommendMovies"
      :key="movie.movieId"
      class="movie-item"
      @click="goToMovieDetail(movie)"
    >
      <img :src="movie.movieImage" alt="海报" class="poster" />
      <div class="movie-info">
        <h3 class="movie-title">{{ movie.movieName }}</h3>
        <div class="movie-score">
          ⭐ {{ movie.movieScore ? movie.movieScore.toFixed(1) : "暂无评分" }}
        </div>
      </div>
    </div>
  </div>
</section>








  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { getPaymentFlowById } from "@/api/user";
import { getRecommendMoviesByUserType } from "@/api/movie";

const route = useRoute();
const router = useRouter();

const loading = ref(true);
const error = ref("");
const flowData = ref(null);

const recommendMovies = ref([]); // 猜你想看 推荐列表

// ✅ 关键修复：从路由 / sessionStorage 双重获取 flowId
const flowId = ref(route.params.flowId || sessionStorage.getItem("flowId") || "");
console.log("获取到的 flowId:", flowId);
// 时间格式化
const formatDate = (d) => (d ? new Date(d).toLocaleString() : "");

// 支付状态映射
const statusText = ref("");
const statusDescription = ref("");
const statusClass = ref("");

const updateStatus = () => {
  if (!flowData.value) return;
  switch (flowData.value.statues) {
    case 1:
      statusText.value = "支付成功";
      statusDescription.value = "订单已完成支付";
      statusClass.value = "success";
      break;
    case 2:
      statusText.value = "已退票（退款中）";
      statusDescription.value = "退款正在处理中";
      statusClass.value = "processing";
      break;
    default:
      statusText.value = "未支付 / 已取消";
      statusDescription.value = "请重新下单或联系客服";
      statusClass.value = "failed";
  }
};

const fetchFlow = async () => {
  // ✅ flowId 缺失时直接给用户提示
  if (!flowId.value) {
    error.value = "未找到支付流水信息，请重新从订单页面进入";
    loading.value = false;
    return;
  }

  try {
    const res = await getPaymentFlowById(flowId.value);
    flowData.value = res.data.data;
    updateStatus();
  } catch {
    error.value = "获取流水失败，请稍后再试";
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  fetchFlow();
  loadRecommendMovies();// 页面加载时获取推荐
});

const goBack = () => router.back();
const goToHome = () => router.push("/reying");


// 加载推荐电影
async function loadRecommendMovies() {
  try {
    const userId = localStorage.getItem("userId") || 1; // ✅ 根据登录情况调整
    const res = await getRecommendMoviesByUserType(userId, 6);
    console.log("🎯 猜你想看接口返回：", res.data); // ✅ 输出 response

    if (res.data.code === 200) {
      recommendMovies.value = res.data.data || [];
    }
  } catch (err) {
    console.error("❌ 推荐电影获取失败:", err);
  }
}
</script>




<style scoped>
/* 样式部分保持不变 */
.payment-flow-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  min-height: calc(100vh - 120px);
  background-color: #f9f9f9;
}

.loading-state {
  text-align: center;
  padding: 60px 0;
}

.spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto 20px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #a2131d;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.error-state {
  text-align: center;
  padding: 60px 0;
  color: #666;
}

.error-icon {
  font-size: 48px;
  margin-bottom: 20px;
}

.error-message {
  font-size: 16px;
  margin-bottom: 30px;
}

.retry-btn {
  padding: 10px 20px;
  background-color: #a2131d;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.flow-container {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  transition: box-shadow 0.3s ease;
}

.flow-container:hover {
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
}

.status-card {
  padding: 30px;
  text-align: center;
  margin-bottom: 0;
  position: relative;
  overflow: hidden;
}

.status-card::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(
    90deg,
    transparent,
    rgba(162, 19, 29, 0.1),
    transparent
  );
}

.status-card.success {
  background-color: #f0fff4;
  border-bottom: 1px solid #c6f6d5;
}

.status-card.processing {
  background-color: #fff8e6;
  border-bottom: 1px solid #ffe8cc;
}

.status-card.failed {
  background-color: #fff5f5;
  border-bottom: 1px solid #fed7d7;
}

.status-icon {
  font-size: 60px;
  margin-bottom: 18px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.status-card.success .status-icon {
  color: #22c55e;
}

.status-card.processing .status-icon {
  color: #f59e0b;
}

.status-card.failed .status-icon {
  color: #ef4444;
}

.status-info h2 {
  margin: 0 0 12px 0;
  font-size: 24px;
  color: #222;
}

.status-info p {
  color: #777;
  margin: 0;
  font-size: 15px;
}

.flow-details {
  padding: 30px;
}

.flow-details h3 {
  font-size: 18px;
  color: #333;
  margin: 0 0 25px;
  padding-bottom: 12px;
  border-bottom: 1px solid #eee;
  position: relative;
}

.flow-details h3::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: -1px;
  width: 40px;
  height: 2px;
  background-color: #a2131d;
}

.detail-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.detail-item {
  display: flex;
  padding: 18px 0;
  border-bottom: 1px solid #eee;
  transition: background-color 0.2s, box-shadow 0.2s;
}

.detail-item:hover {
  background-color: #fafafa;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03);
}

.detail-item:last-child {
  border-bottom: none;
}

.label {
  flex: 0 0 140px;
  color: #666;
  font-size: 14px;
  padding-right: 18px;
  position: relative;
  font-weight: 500;
}

.label::after {
  content: "";
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  height: 14px;
  width: 1px;
  background-color: #eee;
}

.value {
  flex: 1;
  color: #333;
  font-size: 14px;
  word-break: break-all;
  padding-left: 18px;
  font-weight: 400;
}

.value:contains("¥") {
  color: #a2131d;
  font-weight: 500;
}

.action-buttons {
  display: flex;
  gap: 20px;
  padding: 30px;
  border-top: 1px solid #eee;
  justify-content: center;
  background-color: #fafafa;
}

.order-detail-btn,
.home-btn {
  padding: 14px 30px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 15px;
  font-weight: 500;
}

.order-detail-btn {
  background-color: #a2131d;
  color: white;
  border: none;
}

.order-detail-btn:hover {
  background-color: #8a1018;
  transform: translateY(-2px);
  box-shadow: 0 3px 10px rgba(162, 19, 29, 0.25);
}

.home-btn {
  background-color: white;
  color: #333;
  border: 1px solid #ddd;
}

.home-btn:hover {
  background-color: #f5f5f5;
  border-color: #a2131d;
  color: #a2131d;
  transform: translateY(-2px);
  box-shadow: 0 2px 6px rgba(162, 19, 29, 0.1);
}

@media (max-width: 768px) {
  .payment-flow-page {
    padding: 15px;
  }

  .page-header {
    padding: 0 0 15px;
  }

  .header-content {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }

  .page-header h1 {
    font-size: 20px;
    padding-left: 10px;
  }

  .back-btn {
    width: auto;
    align-self: flex-end;
  }

  .detail-item {
    flex-direction: column;
    padding: 12px 0;
  }

  .label {
    flex: none;
    padding-right: 0;
    margin-bottom: 5px;
  }

  .label::after {
    display: none;
  }

  .value {
    padding-left: 0;
  }

  .action-buttons {
    flex-direction: column;
  }

  .order-detail-btn,
  .home-btn {
    width: 100%;
  }
}

@media (min-width: 769px) {
  .header-content {
    align-items: center;
  }
}



/* 猜你想看 */
.guess-section {
  margin-top: 50px;
}
.guess-section .section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.guess-section .section-header h2 {
  font-size: 20px;
  font-weight: bold;
  margin: 0;
}

.guess-section .movies-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.guess-section .movie-item {
  width: 200px;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s ease;
}
.guess-section .movie-item:hover {
  transform: translateY(-6px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18);
}

.guess-section .poster {
  width: 100%;
  height: 280px;
  object-fit: cover;
}

.guess-section .movie-info {
  text-align: center;
  padding: 10px;
}
.guess-section .movie-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.guess-section .movie-score {
  margin-top: 6px;
  color: #e67e22;
  font-weight: bold;
  font-size: 14px;
}

</style>